package model

import (
	"time"
)

type LinkageTask struct {
	LinkageTaskId int64 `xorm:"pk autoincr"`
	CustomerId int64
	CustomerName string
	AccountId int64
	LinkageMessageId int64
	LinkageState int
	EmployeeOrSupervisor int
	TermOfValidity time.Time
	ShoppingReply time.Time
	IsOver int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

func (lkt *LinkageTask)InsertLinkageTask(lktv *LinkageTask) map[string]interface{}{
	x.Insert(lktv)
	return map[string]interface{}{"message": "成功","code":"200"}
}

func (lkt *LinkageTask)UpdateTermOfValidity(lktv *LinkageTask) map[string]interface{}{
	x.Id(lktv.LinkageTaskId).Update(lktv)
	return map[string]interface{}{"message": "成功","code":"200"}
}

func (lkt *LinkageTask)QueryLinkageTask() []*LinkageTask{
	var lktList []*LinkageTask
	x.SQL("select * from linkage_task where term_of_validity <= ? and is_over = ?", time.Now().Format("2006-01-02 15:04:05"), 1).Find(&lktList)
	//x.Where("term_of_validity <= ?", time.Now().Format("2006-01-02 15:04:05")).And("is_over = ?", 1).Find(&lktList)
	return lktList
}

func (lkt *LinkageTask)QueryLinkageTaskById(lkmId int64) *LinkageTask{
	lktv := new(LinkageTask)
	x.Where("linkage_message_id = ?", lkmId).Get(lktv)
	return lktv
}

func (lkt *LinkageTask)QueryLinkageTaskByCustomerId(cId int64) bool{
	var lktvList []*LinkageTask
	x.Where("customer_id = ?", cId).Find(&lktvList)
	var sign bool = true
	for _, v := range lktvList{
		if v.IsOver == 1{
			sign = false
			break
		}
	}
	return sign
}
